package com.da.javatest.leetCode;

/**
 * @author chenlida
 * @date 2019/1/4 14:54
 * @description
 */
public class Code289 {
    public void gameOfLife(int[][] board) {
        if (board[0].length == 0) { return; }
        int max1 = board.length;
        int max2 = board[0].length;
        int[][] temp = new int[max1][max2];
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                temp[i][j] = board[i][j];
            }
        }

        for (int i = 0; i < temp.length; i++) {
            for (int j = 0; j < temp[0].length; j++) {
                int total = 0;
                if (i != 0 && j != 0) { total += temp[i - 1][j - 1]; }
                if (i != 0) { total += temp[i - 1][j]; }
                if (i != 0 && j != max2 - 1) { total += temp[i - 1][j + 1]; }
                if (j != 0) { total += temp[i][j - 1]; }
                if (j != max2 - 1) { total += temp[i][j + 1]; }
                if (i != max1 - 1 && j != 0) { total += temp[i + 1][j - 1]; }
                if (i != max1 - 1) { total += temp[i + 1][j]; }
                if (i != max1 - 1 && j != max2 - 1) { total += temp[i + 1][j + 1]; }
                int ceil = temp[i][j];
                if (ceil == 1 && total < 2) {
                    board[i][j] = 0;
                } else if (ceil == 1 && total > 3) {
                    board[i][j] = 0;
                } else if (ceil == 1) {
                    board[i][j] = 1;
                }
                if (ceil == 0 && total == 3) {board[i][j] = 1;}
            }
        }
    }
}
